﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.IO;
using Kgb.Model.Models;
using Kgb.Web2.Util;
using System.Net.Http;
using System.Web.Http.Routing;

namespace Kgb.Web2.Repository
{
    public class PlayerRepository
    {
        private string restService = ConfigurationManager.AppSettings["restService"];
        private RequestMethod requestMethod = new RequestMethod();
        ISerialization _serializer;

        public PlayerRepository(ISerialization serializer)
        {
            _serializer = serializer;
        }

        public void Update(Player player)
        {
            String json = _serializer.Serialize(player);
            requestMethod.getRequest("PUT", "application/json", string.Format("{0}/players", restService), json).GetResponse();
        }

        public List<Player> Get()
        {
            var responseStream = requestMethod
                                .GetResponseStream(requestMethod
                                                   .getRequest("GET"
                                                              , "application/json"
                                                              , string.Format("{0}/players", restService)
                                                              )
                                                   .GetResponse()
                                                  );

            var players = _serializer.DeSerialize(responseStream) as List<Player>;

            return players;
        }

        public Player Get(int id)
        {
            var responseStream = requestMethod
                                .GetResponseStream(requestMethod
                                                   .getRequest("GET"
                                                              , "application/json"
                                                              , string.Format("{0}/players/{1}", restService, id)
                                                              )
                                                   .GetResponse()
                                                  );

            var player = _serializer.DeSerialize(responseStream) as Player;

            return player;
        }

        public Player Create(Player player)
        {
            String json = _serializer.Serialize(player);
            var r = requestMethod.getRequest("POST", "application/json", string.Format("{0}/players", restService), json).GetResponse();

            var location = r.Headers.Get("Location");

            Uri uri = new Uri(location);

            player.Id = int.Parse(uri.Segments.Last());
            return player;
        }

        public void Delete(int id)
        {
            requestMethod.GetResponseStream(requestMethod
                                            .getRequest("DELETE"
                                                       , "application/json"
                                                       , string.Format("{0}/players/{1}", restService, id)
                                                       )
                                            .GetResponse()
                                           );
        }
    }
}